<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet">
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        secondary: '#10B981',
                        accent: '#F59E0B',
                        neutral: '#1F2937',
                    },
                    fontFamily: {
                        sans: ['Inter', 'system-ui', 'sans-serif'],
                    },
                }
            }
        }
    </script>
    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }
            .form-input-focus {
                @apply focus:border-primary focus:ring-2 focus:ring-primary/20 transition-all duration-200;
            }
            .btn-hover {
                @apply hover:shadow-lg transform hover:-translate-y-1 transition-all duration-300;
            }
        }
    </style>
</head>
<body class="bg-gradient-to-br from-gray-50 to-blue-50 min-h-screen flex items-center justify-center p-4">
    <div class="bg-white rounded-2xl shadow-xl w-full max-w-md overflow-hidden">
        <div class="p-6 md:p-8">
            <div class="text-center mb-8">
                <h2 class="text-[clamp(1.5rem,3vw,2rem)] font-bold text-neutral">创建新账户</h2>
                <p class="text-gray-500 mt-2">加入我们的失物招领平台</p>
            </div>

            <form id="register-form" class="space-y-5">
                <div>
                    <label for="username" class="block text-sm font-medium text-gray-700 mb-1">用户名</label>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fa fa-user text-gray-400"></i>
                        </div>
                        <input type="text" id="username" name="username"
                            class="w-full pl-10 pr-4 py-3 rounded-lg border border-gray-300 form-input-focus"
                            placeholder="请输入用户名" required>
                    </div>
                    <p class="error-message text-red-500 text-xs mt-1 hidden">用户名不能为空</p>
                </div>

                <div>
                    <label for="password" class="block text-sm font-medium text-gray-700 mb-1">密码</label>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fa fa-lock text-gray-400"></i>
                        </div>
                        <input type="password" id="password" name="password"
                            class="w-full pl-10 pr-4 py-3 rounded-lg border border-gray-300 form-input-focus"
                            placeholder="请输入密码" required>
                        <button type="button" id="toggle-password" class="absolute inset-y-0 right-0 pr-3 flex items-center">
                            <i class="fa fa-eye-slash text-gray-400"></i>
                        </button>
                    </div>
                    <p class="error-message text-red-500 text-xs mt-1 hidden">密码长度至少8位</p>
                </div>

                <div>
                    <label for="confirm-password" class="block text-sm font-medium text-gray-700 mb-1">确认密码</label>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fa fa-lock text-gray-400"></i>
                        </div>
                        <input type="password" id="confirm-password" name="confirm-password"
                            class="w-full pl-10 pr-4 py-3 rounded-lg border border-gray-300 form-input-focus"
                            placeholder="请再次输入密码" required>
                    </div>
                    <p class="error-message text-red-500 text-xs mt-1 hidden">两次输入的密码不一致</p>
                </div>

                <div class="pt-2">
                    <button type="submit" id="register-submit"
                        class="w-full bg-primary hover:bg-primary/90 text-white font-medium py-3 px-4 rounded-lg btn-hover flex items-center justify-center">
                        <span>注册账户</span>
                        <i class="fa fa-arrow-right ml-2"></i>
                    </button>
                </div>
            </form>

            <div class="mt-6 text-center">
                <p class="text-gray-600">
                    已有账户? <a href="login.html" class="text-primary hover:underline font-medium">立即登录</a>
                </p>
            </div>
        </div>
    </div>

    <!-- 通知提示框 -->
    <div id="notification" class="fixed top-4 right-4 px-6 py-3 rounded-lg shadow-lg z-50 transform transition-all duration-300 translate-x-full opacity-0 hidden">
        <div class="flex items-center">
            <i class="fa fa-info-circle mr-3 text-lg"></i>
            <span id="notification-message">通知内容</span>
        </div>
    </div>

    <script>
        // DOM元素
        const registerForm = document.getElementById('register-form');
        const usernameInput = document.getElementById('username');
        const passwordInput = document.getElementById('password');
        const confirmPasswordInput = document.getElementById('confirm-password');
        const togglePasswordBtn = document.getElementById('toggle-password');
        const registerSubmitBtn = document.getElementById('register-submit');
        const notification = document.getElementById('notification');
        const notificationMessage = document.getElementById('notification-message');

        // API基础URL
        const API_BASE_URL = 'http://localhost:5000/api';

        // 密码可见性切换
        togglePasswordBtn.addEventListener('click', function() {
            const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
            passwordInput.setAttribute('type', type);

            // 切换图标
            const icon = togglePasswordBtn.querySelector('i');
            if (type === 'text') {
                icon.classList.remove('fa-eye-slash');
                icon.classList.add('fa-eye');
            } else {
                icon.classList.remove('fa-eye');
                icon.classList.add('fa-eye-slash');
            }
        });

        // 表单验证
        function validateForm() {
            let isValid = true;

            // 验证用户名
            if (!usernameInput.value.trim()) {
                showError(usernameInput, '用户名不能为空');
                isValid = false;
            } else {
                hideError(usernameInput);
            }

            // 验证密码
            if (passwordInput.value.length < 8) {
                showError(passwordInput, '密码长度至少8位');
                isValid = false;
            } else {
                hideError(passwordInput);
            }

            // 验证确认密码
            if (confirmPasswordInput.value !== passwordInput.value) {
                showError(confirmPasswordInput, '两次输入的密码不一致');
                isValid = false;
            } else {
                hideError(confirmPasswordInput);
            }

            return isValid;
        }

        // 显示错误信息
        function showError(input, message) {
            const formGroup = input.parentElement.parentElement;
            const errorMessage = formGroup.querySelector('.error-message');

            input.classList.add('border-red-500');
            errorMessage.textContent = message;
            errorMessage.classList.remove('hidden');
        }

        // 隐藏错误信息
        function hideError(input) {
            const formGroup = input.parentElement.parentElement;
            const errorMessage = formGroup.querySelector('.error-message');

            input.classList.remove('border-red-500');
            errorMessage.classList.add('hidden');
        }

        // 显示通知
        function showNotification(message, type = 'info') {
            // 设置通知类型
            notification.className = `fixed top-4 right-4 px-6 py-3 rounded-lg shadow-lg z-50 transform transition-all duration-300 translate-x-full opacity-0 ${
                type === 'success' ? 'bg-green-50 text-green-800 border-l-4 border-green-400' :
                type === 'error' ? 'bg-red-50 text-red-800 border-l-4 border-red-400' :
                'bg-blue-50 text-blue-800 border-l-4 border-blue-400'
            }`;

            // 设置图标
            const icon = notification.querySelector('i');
            if (type === 'success') {
                icon.className = 'fa fa-check-circle mr-3 text-lg';
            } else if (type === 'error') {
                icon.className = 'fa fa-exclamation-circle mr-3 text-lg';
            } else {
                icon.className = 'fa fa-info-circle mr-3 text-lg';
            }

            // 设置消息
            notificationMessage.textContent = message;

            // 显示通知
            notification.classList.remove('hidden');
            setTimeout(() => {
                notification.classList.remove('translate-x-full', 'opacity-0');
            }, 10);

            // 自动关闭
            setTimeout(() => {
                notification.classList.add('translate-x-full', 'opacity-0');
                setTimeout(() => {
                    notification.classList.add('hidden');
                }, 300);
            }, 3000);
        }

        // 注册表单提交
        registerForm.addEventListener('submit', async function(e) {
            e.preventDefault();

            if (!validateForm()) {
                return;
            }

            // 显示加载状态
            registerSubmitBtn.disabled = true;
            registerSubmitBtn.innerHTML = '<i class="fa fa-spinner fa-spin mr-2"></i>注册中...';

            try {
                const response = await fetch(`${API_BASE_URL}/register`, {
                    method: 'POST',
                    credentials: 'include',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        username: usernameInput.value.trim(),
                        password: passwordInput.value
                    })
                });

                if (response.ok) {
                    showNotification('注册成功！即将跳转到登录页面...', 'success');

                    // 注册成功后延迟跳转
                    setTimeout(() => {
                        window.location.href = 'login.html';
                    }, 2000);
                } else {
                    const errorData = await response.json();
                    throw new Error(errorData.error || '注册失败，请重试');
                }
            } catch (error) {
                showNotification(error.message, 'error');
            } finally {
                // 恢复按钮状态
                registerSubmitBtn.disabled = false;
                registerSubmitBtn.innerHTML = '<span>注册账户</span><i class="fa fa-arrow-right ml-2"></i>';
            }
        });

        // 输入字段实时验证
        usernameInput.addEventListener('input', function() {
            if (this.value.trim()) {
                hideError(this);
            }
        });

        passwordInput.addEventListener('input', function() {
            if (this.value.length >= 8) {
                hideError(this);
            }

            // 检查确认密码
            if (confirmPasswordInput.value) {
                if (confirmPasswordInput.value === this.value) {
                    hideError(confirmPasswordInput);
                } else {
                    showError(confirmPasswordInput, '两次输入的密码不一致');
                }
            }
        });

        confirmPasswordInput.addEventListener('input', function() {
            if (this.value === passwordInput.value) {
                hideError(this);
            } else {
                showError(this, '两次输入的密码不一致');
            }
        });
    </script>
</body>
</html>